﻿using System;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;

public partial class EmailToFriend : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        SendEmailToResponder();
    }

    void SendEmailToResponder()
    {
        // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0

        // System.Net.Mail.SmtpClient is the alternate class for this in 2.0

        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();

        try
        {
            // To address collection of MailAddress
            smtpClient.Credentials = new NetworkCredential("noreply@dailyextranews.com", "dailyextra");
            smtpClient.Host = "mail.dailyextranews.com";
            //smtpClient.UseDefaultCredentials = true;
            //smtpClient.EnableSsl = true;


            MailAddress fromAddress = new MailAddress("noreply@dailyextranews.com");
            message.From = fromAddress;

            //fromAddress.DisplayName = "Daily azad riyast";


            message.To.Add(new MailAddress(txtFriendEmail.Text, txtFriendName.Text));
            //message.CC.Add(new MailAddress("info@dailyextranews.com", "Daily Extra News"));

            message.Subject = "Your friend wants you to view a News page on Daily Extra News Website!"; //"Feedback";

            // CC and BCC optional
            // MailAddressCollection class is used to send the email to various users
            // You can specify Address as new MailAddress("admin1@yoursite.com")

            //message.CC.Add("khurram_khan123@hotmail.com");
            message.Bcc.Add("khurram_khan123@hotmail.com");


            //Body can be Html or text format
            //Specify true if it  is html message
            message.IsBodyHtml = true;


            String url = Server.UrlDecode(Request.QueryString["url"]);
            String title = Server.UrlDecode(Request.QueryString["title"]);
            title = title.Replace("Daily Extra News | ", string.Empty);
            title = title.Replace("|", "-");

            StringBuilder body = new StringBuilder();


            body.AppendFormat(@"Dear {0},<br><br>
                            Your friend <b> {1}&lt;{2}&gt;</b> wants you to View following page on Daily Extra News.<br><br>
                            <a href='{3}'>Views news of {4}</a><br><br>
                            If above link does not work then copy and paste following url in your browser:<br>
                            {5}<br><br><br>
                            ---<br>
                            This message was sent by Sender email adress via Daily Extra News.  Please note that AddThis does not verify email addresses.<br>
 
                            Make sharing easier with the AddThis Toolbar:  http://www.addthis.com/go/toolbar-em <br>
 
                            To stop receiving any emails from AddThis please visit: http://www.addthis.com/privacy/email-opt-out?e=AAtxc25lY3dld2d_OidLfmRiZndieiV1ZHs in your web browser.",
                                                                                                                                                                        
                            txtFriendName.Text, txtName.Text, txtEmail.Text, url, title, url);






            //body.AppendLine("Hello,<br>");
            //body.AppendLine("Your friend <b>\"" + txtName.Text + "\" &lt;" + txtEmail.Text + "&gt;</b> wants you to View following page on Daily Extra News.");
            //body.AppendLine("<br><br>");
            //body.AppendLine("<a href=\"" + url + "\">Views news of " + title + "</a>");
            //body.AppendLine("<br><br>");
            //body.AppendLine("If above link does not work then copy and paste following url in your browser:<br>");
            //body.AppendLine(url);
            //body.AppendLine("<br><br>");

            // Message body content
            message.Body = body.ToString();

            // Send SMTP mail

            smtpClient.Send(message);

            Panel1.Visible = false;
            ThankYouPanel.Visible = true;

            //lblStatus.Text = "Email successfully sent.";
        }
        catch (Exception ex)
        {
            //  Code that runs on application shutdown
            Elmah.Error error = new Elmah.Error(ex);
            //Elmah.ErrorLog.Default.Log(error);
            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(error);
            //lblStatus.Text = "Send Email Failed." + ex.Message;
        }
    }
}